<?php 
error_reporting(0);
$isadmin=1;
include '../../comm/config.php';
include '../../comm/checkpostandget.php';
include '../check.php';

function insert_one_sql($table,$filed_arr,$alert=0){ 
    global $BIAOTOU;
	$field="";
	$values="";
	foreach($filed_arr as $k=>$v){
		$field = "`".$k."`,".$field;
		$values = "'".$v."',".$values;
		}
	$field=substr($field,0,strlen($field)-1);
	$values=substr($values,0,strlen($values)-1);
	$sql="insert into $BIAOTOU$table($field) values ($values);";
	$query=mysql_query($sql);
	if($alert==0){
	    if($query) return 1;
	    else return mysql_error();
	}
	elseif($alert==1){
	    echo $sql;
	}
	elseif($alert==2){
		echo $sql;
	    if($query) return 1;
	    else return mysql_error();
	}
}

function sel_sql($table,$sel_field,$where,$alert=0){
	global $BIAOTOU;
	$sql="select $sel_field from $BIAOTOU$table where $where";
	if($alert==1){
	    echo $sql;
	}
	$query=mysql_query($sql);
	if($query){
		while($row=mysql_fetch_array($query)){
			if(strpos($sel_field,",") or $sel_field=="*"){
			    $arr=$row;
			}
			else{
			    $arr=$row[$sel_field];
			}
		}
	}
	return $arr;	
}

function alert($word,$url=0){
	if($url===0){
		return "<script>alert('".$word."');history.go(-1);</script>";
	}
	else{
	    return "<script>alert('".$word."');window.location='".$url."';</script>";
	}
}

function inc_fanli($ddusername, $fxje, $tgje, $order_code,$merchant_id) {
	$field_arr=array('money'=>$fxje,'dengji'=>1);
	update_sql('user', $field_arr, "ddusername='$ddusername'", 1); //增加会员金额和等级

	//用户消息
	$title = '您获得了新的商城交易返现！';
	$trade_id = $order_code;
	$msg_tabao = "您获得了新的交易返现，".$merchant_id."商城订单号" . $order_code . "返现金额" . $fxje;
	$filed_arr = array (
		'title' => $title,
		'content' => $msg_tabao,
		'addtime' => date('Y-m-d H:i:s'
	), 'see' => 0, 'ddusername' => $ddusername, 'senduser' => '网站客服');
	insert_one_sql("msg", $filed_arr);

	//用户明细
	$shijian = "商城交易返现";
	$memo = $merchant_id."交易号$order_code";
	$filed_arr = array (
		'ddusername' => $ddusername,
		'shijian' => $shijian,
		'addtime' => date('Y-m-d H:i:s'
	), 'je' => $fxje, 'memo' => $memo);
	insert_one_sql("mingxi", $filed_arr);

	//求推荐人
	$tjrid = sel_sql("user", "tjr", "ddusername='$ddusername'");
	if ($tjrid > 0) {
		$tjrname = sel_sql("user", "ddusername", "Id='$tjrid'");
		//增加推荐人佣金
		$field_arr = array (
			'money' => $tgje
		);
		update_sql("user", $field_arr, "Id='$tjrid'", 1);

		//用户消息
		$title = '您获得了新的推广佣金！';
		$msg_taobaotuiguang = "您获得了新的推广佣金" . $tgje;
		$filed_arr = array (
			'title' => $title,
			'content' => $msg_taobaotuiguang,
			'addtime' => date('Y-m-d H:i:s'
		), 'see' => 0, 'ddusername' => $tjrname, 'senduser' => '网站客服');
		insert_one_sql("msg", $filed_arr);

		//用户明细
		$shijian = "推广佣金";
		$memo = "交易人$ddusername";
		$filed_arr = array (
			'ddusername' => $tjrname,
			'shijian' => $shijian,
			'addtime' => date('Y-m-d H:i:s'
		), 'je' => $tgje, 'memo' => $memo);
		insert_one_sql("mingxi", $filed_arr);
	}
}

function check_empty($arr){ //数组加addslashes
	foreach ($arr as $key => $value) {
        if (is_array($value)) {
            check_empty($arr[$key]);
        } else {
            $value = trim($value);
            if($value == ''){
		        return 0;
	        }
        }
    }
	return 1;
}

$link = mysql_connect($dbserver, $dbuser, $dbpass);
if (!$link) {
	die(ERR_DB);
}
else
{
	mysql_query("set names utf8");
	mysql_select_db($dbname);
	$info=$_POST['info'];
	if($_POST['Submit']){
		
		if($info['u_id']!=0){
		    $dduser=sel_sql(user,'ddusername,dengji',"Id='".$info['u_id']."'",0);
	        $info['ddusername']=$dduser['ddusername'];
		    if(!$info['ddusername']){
		        echo alert('会员不存在','addmallorders.php');
			    exit;
		    }
		}
		else{
		    $info['ddusername']='';
			$dduser['dengji']=0;
		}
	    
		if($info['lm']=='yiqifa'){
		    $id=sel_sql('mall_order','id',"unique_id='".$info['unique_id']."'",0);
			if($id>0){
			    echo alert('订单已存在','addmallorders.php');
				exit;
			}
		}
		elseif($info['lm']=='linktech'){
		    $id=sel_sql('mall_order','id',"order_code='".$info['order_code']."' and product_code='".$info['product_code']."'",0);
			if($id>0){
			    echo alert('订单已存在','addmallorders.php');
				exit;
			}
		}
		$info['sales']=$info['item_count']*$info['item_price'];
	    $info['fxje'] = mall_ck_fxje($info['commission'], $dduser['dengji']);
		if($info['stat_desc']=='核对有效'){
		    $info['qrsj']=date('Y-m-d H:i:s');
			$tgje=$fxje*TGBL;
			inc_fanli($info['ddusername'], $info['fxje'], $tgje, $info['order_code'],$info['merchant_id']);
		}
	    insert_one_sql('mall_order',$info,0);
		echo alert('添加成功','addmallorders.php');
	}
}	
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>管理中心</title>
<style> 
.disable{border:#999 1px solid; background:#CCC;}
</style>
<link href="../css/style.css" rel="stylesheet" type="text/css">
<script language="javascript" type="text/javascript" src="../../js/jquery.js"></script>
<script language="javascript" type="text/javascript">	
$(function(){
    $('#lm').change(function(){
	    var lm = $(this).val();
	    if(lm=='yiqifa'){
			$('#yilink').attr('readonly','').removeClass('disable').removeClass('required');
			$('#merchant').removeClass('required');
			//$('#chalink').attr('readonly','readonly').addClass('disable');
	    }
		if(lm=='linktech'){
			$('#merchant').addClass('required');
			$('#yilink').attr('readonly','readonly').addClass('disable').removeClass('required');
			//$('#chanet').attr('readonly','readonly').addClass('disable');
	    }
	});
	
	$('#form1').submit(function(){
		var subm=1;
	    $('.required').each(function(){
		    if($(this).val()==''){
			    $(this).focus();
				alert('该字段不能为空');
				subm=0;
				return false;
			}
		});
		if(subm==0) return false;
		if(subm==1) return true;
	});
});


</script>
</head>

<body topmargin="5" leftmargin="0">
<table width="98%" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td height="282" valign="top"><table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
      <tr>
        <td>
          <table cellspacing="0" width="100%" height="207" style="border:1px  solid #DCEAF7;">
            <tr>
              <td height="30" colspan="2" bgcolor="E9F2FB" class="bigtext"><strong>&nbsp;<img src="../images/arrow.gif" width="16" height="22" align="absmiddle" /> 确认商城订单</strong></td>
            </tr>
            <tr>
              <td width="100%" colspan="2" valign="top"><table width="100%" height="300" border="0" cellpadding="0" cellspacing="0">
                <tr>
                  <td valign="top"><form id="form1" name="form1" method="post" action="">
<br />
<table width="720"  align="center" border=1 cellpadding=0 cellspacing=0
style="border-collapse: collapse" bordercolor="#dddddd">
                      
					  <?php $row = mysql_fetch_array($rs);?>
                      <tr>
                        <td width="200" height="30" align="right" class="bigtext">下单时间：                        </td>
                        <td width="577" height="30" class="bigtext">&nbsp;<input class="required" id="order_time" type="text" name="info[order_time]" />(格式：2011-01-01 12:12:12)</td>
                      </tr>
                      <tr>
                        <td height="30" align="right" class="bigtext">商城名：                        </td>
                        <td width="577" height="30" class="bigtext">&nbsp;<input class="required" id="merchant_id" type="text" name="info[merchant_id]" />(参照现有订单)</td>
                      </tr>
                      <tr>
                        <td height="30" align="right" class="bigtext">会员id(没有就写0)：                        </td>
                        <td width="577" height="30" class="bigtext">&nbsp;<input class="required" id="ddusername" type="text" name="info[u_id]" />(亿起发叫：反馈标签 | 领客特叫U_ID)</td>
                      </tr>
					   <tr>
                        <td height="30" align="right" class="bigtext">订单号(亿起发)订货号(领客特)：</td>
                        <td height="30" class="bigtext">&nbsp;<input class="required" id="order_code" type="text" name="info[order_code]" /></td>
                      </tr>
                      <tr>
                        <td height="30" align="right" class="bigtext">商品编号：</td>
                        <td height="30" class="bigtext">&nbsp;<input class="" id="merchant" type="text" name="info[product_code]"/>(亿起发有就填，没有就不填，领客特必填)</td>
                      </tr>
					   <tr>
                        <td height="30" align="right" class="bigtext">数量：</td>
                        <td height="30" class="bigtext">&nbsp;<input class="required" id="item_count" type="text" name="info[item_count]" /></td>
                      </tr>
					   <tr>
                        <td height="30" align="right" class="bigtext">单价：</td>
                        <td height="30" class="bigtext">&nbsp;<input class="required" id="item_price" type="text" name="info[item_price]" /></td>
                      </tr>
                      <tr>
                        <td height="30" align="right" class="bigtext">佣金：</td>
                        <td height="30" class="bigtext">&nbsp;<input class="required" id="commission" type="text" name="info[commission]" /></td>
                      </tr>
					  <tr>
                        <td height="30" align="right" class="bigtext">订单状态：</td>
                        <td height="30" class="bigtext">&nbsp;<select name="info[stat_desc]"><option value="未核对">未核对</option><option value="核对有效">核对有效</option></select></td>
                      </tr>
                      <tr>
                        <td height="30" align="right" class="bigtext">联盟：</td>
                        <td height="30" class="bigtext">&nbsp;<select id="lm" name="info[lm]"><option value="yiqifa">亿起发</option><option value="linktech">领客特</option></select></td>
                      </tr>
                      <tr>
                        <td height="30" align="right" class="bigtext">唯一编号(亿起发)：</td>
                        <td height="30" class="bigtext">&nbsp;<input class="required" id="yilink" type="text" name="info[unique_id]" /></td>
                      </tr>
                      
                      
					  
					   <tr>
                        <td height="47" align="center" class="bigtext">&nbsp;</td>
                        <td height="47" class="bigtext">
                          &nbsp;<input type="submit" name="Submit" value=" 确定 "  /></td>
					   </tr>
                    </table>
                                    </form>
                  </td>
                </tr>
              </table></td>
            </tr>
          </table></td>
        </tr>
      
    </table></td>
  </tr>
</table>
</body>
</html>
<?php mysql_close($link);?>
